import DialogBaseUI from "../../custom/DialogBaseUI";
import {EndType, MAX_PLAYER} from "./public/GameDefine";
import {PlayerData} from "../PlayerData";
import {ToolUtils} from "../../util/ToolUtils";

const {ccclass, property} = cc._decorator;
@ccclass
export default class GameEndUI extends DialogBaseUI {
    @property([cc.Node])
    playerEnds:Array<cc.Node> = [];
    @property(cc.Sprite)
    endType:cc.Sprite = null;
    show(msg){
        let win_flag = 0;
        let heroSeat = PlayerData.getHero().seat;
        if(msg.rank[0] == heroSeat || msg.rank[0] == (heroSeat+2)%MAX_PLAYER) win_flag=1;
        //this.endType.spriteFrame = new cc.SpriteFrame(cc.url.raw("game/shuangkou/txt_end_type_"+msg.endType+"_"+win_flag+".png"));
        var self = this;
        cc.resources.load("game/shuangkou/txt_end_type_"+msg.endType+"_"+win_flag, cc.SpriteFrame, function (err, spriteFrame:cc.SpriteFrame) {
            self.endType.spriteFrame = spriteFrame;
        });
        for(let i=0;i<msg.score.length;i++){
            let localSeat = PlayerData.serverSeatToLocalSeat(i);
            let player = PlayerData.getPlayerByServerSeat(i);
            let score:cc.Label = cc.find("score",this.playerEnds[localSeat]).getComponent(cc.Label);
            let name:cc.Label = cc.find("name",this.playerEnds[localSeat]).getComponent(cc.Label);
            let head:cc.Sprite = cc.find("name",this.playerEnds[localSeat]).getComponent(cc.Sprite);
            let rank:cc.Node = cc.find("rank",this.playerEnds[localSeat]);
            if(msg.score[i]>0){
                score.string = "+"+msg.score[i];
            }else{
                score.string = msg.score[i];
            }
            name.string = player.nickname;
            //ToolUtils.loadNetImage(player.headimg,head);
            rank.active=false;
        }

        for(let i=0;i<msg.rank.length;i++){
            let localSeat = PlayerData.serverSeatToLocalSeat(msg.rank[i]);
            let rankSp:cc.Sprite = cc.find("rank",this.playerEnds[localSeat]).getComponent(cc.Sprite);
            //rankSp.spriteFrame = new cc.SpriteFrame(cc.url.raw("game/shuangkou/rank_"+i+".png"));
            cc.resources.load("game/shuangkou/rank_"+i, cc.SpriteFrame, function (err, spriteFrame:cc.SpriteFrame) {
                rankSp.spriteFrame = spriteFrame;
            });
            rankSp.node.active=true;
        }
        this.node.active = true;
    }
}